<?php
/*
---------------------------------------------------------
														+
名称:易通支付通道										+
														+
控制器名称:YiTongController								+
														+
日期:2017-10-11											+
														+
作者:小翁												+
														+
---------------------------------------------------------
*/
namespace Pay\Controller;

use Common\Common\Api\Order;
use Common\Common\Api\Ordermember;
use Common\Common\Api\Redis\Channel;
use Common\Common\Api\Redis\Product;

/**
 * 麦口微信支付
 * Class Maikou1Controller
 * @package Pay\Controller
 */
class Maikou1Controller extends PayController{
    protected $config;
    protected $key;
    protected $url;
    protected $class;


    public function __construct(){
        parent::__construct();
        $classes = explode("\\",__CLASS__);
        $class = end($classes);
        $this->class = substr($class, 0, stripos($class, "Controller"));

    }

    public function pay($array){
        $this->config = Channel::getInstance()->getById($array['channel']);
        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>C("THIRDCODES.".$this->class)]);
        }


        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $return = $this->orderadd( $parameter );

        switch($this->config['paytype']){
            //微信扫码
            case 1:
                $pay_bankcode = 'mobile_fee_qrcode';
                break;
            //微信H5
            case 2:
                $pay_bankcode = 'mobile_fee_h5';
                break;
            default:
                $this->showmessage('不支持此产品!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $parameter = array(
            "app_id" => $this->config['mch_id'],
            "app_secret" => $this->config['signkey'],
            "price" => intval($return['amount']*100),
            "count" => 1,
            "goods_id" => 176,
            "third_param" => $return['orderid'],
            "notify_url" => $return['notifyurl'],
            //"sub_id" => '',
            "channel" => $pay_bankcode,
        );

        //苹果
        if($this->iosOrAndroid()){
            $parameter["ios"] = 1;
        }

        $res = curl($this->config['gateway'], httpstr($parameter));//curl_post
        $resp = json_decode($res,true);

        $code = $resp['data']['path'];

        if(!$code){
            if($resp['msg']){
                $this->showmessage($resp['msg'], ['code' => C("THIRDCODES." . $this->class)]);
            }
            $this->showmessage('未获取到支付链接!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $ismobile = isMobile();
        $paytype = transArr( C("PAYTYPES"), "id");
        $this->assign("wap_url", $code);
        $this->assign("title", $paytype[$this->config['paytype']]);
        $this->assign("return", $return);
        $this->assign("code_url", $code);
        switch($this->config['paytype']){
            //微信扫码
            case 1:
                if($ismobile){
                    $this->display("WeiXin/weixinwap");
                }else{
                    $this->display("WeiXin/weixin");
                }
                break;
            //微信H5
            case 2:
                header("location: {$code}"); exit;
            default:
                $this->display("WeiXin/Pay");//收银台
        }
    }

    //通知
    public function notifyurl(){
        //$this->debug_callback();
        if(!$this->checkip(getIP(),['106.14.141.160'])){
            exit("wrong");
        }

        $p = I('post.');

        //获取订单信息
        $orderid = $p['third_param'];
        $find = $this->checkOrderNotify($orderid);

        if(!$find){
            echo 'order_not_found'; exit;
        }
        //保存回调数据
        M("Order")->where(array("pay_orderid"=>$orderid))->save(array(

            "pay_orderid_rd"=>htmlspecialchars($p['order_sn'])));

        //获取渠道信息
        $this->config = M("Channel")->where(array('code'=>$this->class))->find();

        $sign = $p['sign'];

        //sign=md5(order_sn+total_price+third_param+app_key)
        $sign1 = md5($p['order_sn'].$p['total_price'].$p['third_param'].$this->config['appid']);


        if($sign1 !== $sign ){
            exit("sign_wrong");
        }

        if($p['goods_id'] !== '176'){
            exit("pay_fail");
        }

        $this->async($find['pay_orderid'],json_encode(["code"=>0,"msg"=>"支付成功","data"=>[]]));
    }

    public function callBackUrl(){

        echo '处理成功'; exit;
    }


    public function iosOrAndroid(){
        if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
            $type = true;
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
            $type = false;
        }else{
            $type = false;
        }

        return $type;
    }
}